package org.jiajie.coffeecodes.concurrent.pattern.guarded.suspension;

import java.util.LinkedList;

/**
 * @author jie
 * @date 2021/8/31 10:28 PM
 */
public class RequestQueue {

    private final LinkedList<Request> queue = new LinkedList<Request>();

    public Request getRequest(){
        synchronized (queue){
            while (queue.size() <= 0){
                try {
                    queue.wait();
                } catch (InterruptedException e){
                    return null;
                }
            }
            return queue.removeFirst();
        }
    }

    public void putRequest(Request request){
        synchronized (queue) {
            queue.addLast(request);
            queue.notifyAll();
        }
    }
}
